package com.west.xu.jdbc.service.impl;

import com.alibaba.druid.pool.DruidDataSource;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.west.xu.jdbc.base.BulkInsertService;
import com.west.xu.jdbc.domain.User;
import com.west.xu.jdbc.mapper.UserMapper;
import com.west.xu.jdbc.service.IUserService;
import com.west.xu.jdbc.utils.StringUtilTemporary;
import org.apache.commons.lang.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {


    @Autowired
    private DruidDataSource dataSource;

    @Override
    public Integer saveUser(User user) throws RuntimeException {
        user.setUserId(RandomStringUtils.randomAlphanumeric(20));
        return baseMapper.insert(user);
    }

    @Override
    public Integer saveUsers(List<String> userJsonList) throws RuntimeException {
        List<String> insertFields = Arrays.asList(StringUtilTemporary.getTableInsertColumns(User.class));
        return BulkInsertService.bulkInsert(dataSource,
                User.TABLE_NAME, User.class, userJsonList, insertFields);
    }

    @Override
    public User selectUser(Long id) throws RuntimeException {
        return baseMapper.selectById(id);
    }

}
